package com.golden.order02.util;

import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

/**
 * 绘制透明背景的文字图片
 * 
 */
public class ImageUtils {
	private static BufferedImage drawTranslucentStringPic(int width, int height, Integer fontHeight, String drawStr) {
		try {
			BufferedImage buffImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics2D gd = buffImg.createGraphics();
			// 设置透明 start
			buffImg = gd.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
			gd = buffImg.createGraphics();
			// 设置透明 end
			gd.setFont(new Font("宋体", Font.PLAIN, fontHeight)); // 设置字体
			gd.setColor(Color.BLACK); // 设置颜色
			// gd.drawRect(0, 0, width - 1, height - 1); //画边框
			gd.drawString(drawStr, width / 2 - fontHeight * drawStr.length() / 2, fontHeight); // 输出文字（中文横向居中）
			return buffImg;
		} catch (Exception e) {
			return null;
		}
	}

	public static boolean dateImage(String data, String filePath) {

		BufferedImage imgMap = drawTranslucentStringPic(400, 40, 30, data);
		File imgFile = new File(filePath);
		try {
			ImageIO.write(imgMap, "PNG", imgFile);
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		}

		return true;
	}
	
}
